package com.simons.common.utils.json;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@SuppressWarnings("unchecked")
public class GsonUtil {
	
	private GsonUtil(){
		// Do nothing because of 不能创建对象
	}

	//设置时间转化格式
	public static final Gson GSON = new GsonBuilder().serializeNulls().registerTypeAdapter(Long.class, new LongSerializer()).registerTypeAdapter(Date.class, new DateSerializer()).create();
	
	public static Map<String, Object> Object2Map(Object obj){
		if(obj == null){
			return new HashMap<String,Object>();
		}
		
		return GSON.fromJson(GSON.toJson(obj), Map.class);
	}
	
	public static Map<String, String> Object2MapString(Object obj){
		if(obj == null){
			return new HashMap<String,String>();
		}
		return GSON.fromJson(GSON.toJson(obj), Map.class);
	}
	
}
